package com.teng.mylibrary.ui.activity

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.teng.mylibrary.Constants
import com.teng.mylibrary.util.SPUtil
import com.teng.mylibrary.net.RetrofitHelper
import com.teng.mylibrary.base.BaseActivity
import com.teng.mylibrary.R
import com.teng.mylibrary.databinding.ActivitySystemConfigBinding
import com.teng.mylibrary.entity.EventMsg
import com.teng.mylibrary.ui.adapter.IPAddressAdapter
import com.teng.mylibrary.util.AdapterFactory
import com.teng.mylibrary.widget.IPAddDialog
import com.teng.mylibrary.widget.InteractionDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Description:    IP配置
 * Author:         liuteng
 * CreateDate:     2021年12月3日
 * UpdateRemark:   更新说明
 */

@Route(path = "/library/SystemConfigActivity")
class SystemConfigActivity : BaseActivity<ActivitySystemConfigBinding, SystemConfigVModel>() {

    val adapter by lazy {
        IPAddressAdapter(viewModel.ipList)
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_system_config
    }

    override fun initViewModel(): SystemConfigVModel {
        return ViewModelProvider(this).get(SystemConfigVModel::class.java)
    }

    override fun initView() {
        title = "系统配置"
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        binding.viewmodel = viewModel
        viewModel.ipList.addOnListChangedCallback(AdapterFactory.getListChangedCallback(adapter))
        binding.cbExit.setOnCheckedChangeListener { buttonView, isChecked ->
            viewModel.doExitAble.set(isChecked)
            if (isChecked) {
                binding.llTime.visibility = View.VISIBLE
            } else {
                binding.llTime.visibility = View.GONE
            }
        }
        viewModel.myUc.saveNotify.observe(this, {
            RetrofitHelper.init(this, SPUtil.getIp())
            showToastOkMsg("保存成功")
            finish()
        })
    }


    override fun initData() {
        if (SPUtil.getIpList().isEmpty()) {
            viewModel.ipList.add(SPUtil.getIp())
        } else {
            val array = Gson().fromJson(SPUtil.getIpList(), Array<String>::class.java)
            viewModel.ipList.addAll(array)
        }
        initRecycler()
        viewModel.timeoutTime.set(SPUtil.getConnectTimeOut())
        viewModel.doExitAble.set(SPUtil.isDoExit())
        viewModel.exitTime.set(SPUtil.getDoExitTime())
        binding.cbExit.isChecked = SPUtil.isDoExit()
        if (viewModel.doExitAble.get() == true) {
            binding.llTime.visibility = View.VISIBLE
        } else {
            binding.llTime.visibility = View.GONE
        }

    }

    private fun initRecycler() {
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter
        adapter.setOnItemClickListener {
            SPUtil.setIp(viewModel.ipList[it])
            adapter.refreshData(viewModel.ipList)
        }
        adapter.setOnItemLongClickListener {
            if (viewModel.ipList[it] != SPUtil.getIp()) {
                val dialog = InteractionDialog("是否删除该地址？")
                dialog.setOnConfirmClickListener {
                    viewModel.ipList.removeAt(it)
                }
                dialog.showNow(supportFragmentManager, null)
            } else {
                showProMsg("已选中的不能删除")
            }
        }
    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_addip, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                onBackPressed()
            }
            R.id.item_addip -> {
                val dialog = IPAddDialog()
                dialog.setOnItemOkListener {
                    if (viewModel.ipList.contains(it)) {
                        showProMsg("该IP已添加至列表")
                    } else {
                        viewModel.ipList.add(it)
                    }
                }
                dialog.showNow(supportFragmentManager, null)
            }
        }
        return super.onOptionsItemSelected(item)
    }


}